package com.ryan.demo.grpc.server.config;

import com.ryan.demo.grpc.server.provider.SubAckHubGrpcServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;

/**
 * @description： RelianGrpcServerConfig
 * @Author MRyan
 * @Date 2023/10/7 22:49
 * @Version 1.0
 */
@Configuration
@Slf4j
public class RelianGrpcServerConfig {

    private Server server;

    @PostConstruct
    public void start() throws IOException {
        this.server = ServerBuilder.forPort(9091)  // 选择一个端口号
                .addService(new SubAckHubGrpcServiceImpl())
                .build()
                .start();
        log.info("Server started, listening on " + 9091);
    }

    @PreDestroy
    public void stop() {
        if (server != null) {
            server.shutdown();
        }
    }
}